package com.itheima.server.controller;

import com.itheima.domain.vo.PageResult;
import com.itheima.domain.vo.VideoVo;
import com.itheima.server.service.SmallVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * @author 阿鹏
 * @date 2021/1/30 21:14
 */
@RestController
@RequestMapping("/smallVideos")
public class SmallVideoController {

    @Autowired
    private SmallVideoService smallVideoService;

    @PostMapping
    public ResponseEntity saveSmallVideos(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        return smallVideoService.saveSmallVideos(videoThumbnail, videoFile);
    }

    @GetMapping
    public ResponseEntity getSmallVideosList(@RequestParam(defaultValue = "1") Integer page,
                                             @RequestParam(defaultValue = "10") Integer pagesize) {
        if (page == 0) {
            page = 1;
        }
        PageResult<VideoVo> pageResult = smallVideoService.getSmallVideosList(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    @PostMapping("{followUserId}/userFocus")
    public ResponseEntity saveUserFocus(@PathVariable("followUserId") long followUserId){
        return smallVideoService.saveUserFocus(followUserId);
    }

    @PostMapping("/{followUserId}/userUnFocus")
    public ResponseEntity removeUserFocus(@PathVariable("followUserId") long followUserId) {
        return smallVideoService.removeUserFocus(followUserId);
    }

    @PostMapping("{id}/like")
    public ResponseEntity saveLike(@PathVariable("id") String videoId) {
        return smallVideoService.saveLike(videoId);
    }

    @PostMapping("{id}/dislike")
    public ResponseEntity removeLike(@PathVariable("id") String videoId) {
        return smallVideoService.removeLike(videoId);
    }

    @PostMapping("{id}/comments")
    public ResponseEntity saveComments(@PathVariable("id") String videoId,
                                       @RequestBody Map map) {
        String content = (String) map.get("comment");
        return smallVideoService.saveComments(videoId, content);
    }

    @GetMapping("{id}/comments")
    public ResponseEntity queryCommentList(@PathVariable("id") String videoId,
                                           @RequestParam(defaultValue = "1") int page,
                                           @RequestParam(defaultValue = "10") int pagesize) {
        return smallVideoService.queryCommentList(videoId, page, pagesize);
    }

    @PostMapping("comments/{id}/like")
    public ResponseEntity saveCommentLike(@PathVariable("id") String commentId) {
        return smallVideoService.saveCommentLike(commentId);
    }

    @PostMapping("comments/{id}/dislike")
    public ResponseEntity removeCommentLike(@PathVariable("id") String commentId) {
        return smallVideoService.removeCommentLike(commentId);
    }


}
